local extension = Package:new("jin_ld_heg")
extension.extensionName = "new_heg"
extension.game_modes_whitelist = { 'mul_heg_mode' }

local H = require "packages/new_heg/util"
local U = require "packages/utility/utility"

Fk:loadTranslationTable{
  ["jin_ld_heg"] = "受命于天DIY",
}

local duyu = General(extension, "ld__n_duyu", "jinz", 4)
duyu.mainMaxHpAdjustedValue = -1
duyu:addCompanions("ld__n_yanghuS")
local shujian = fk.CreateTriggerSkill{
  name = "ld__n_shujian",
  anim_type = "special",
  events = {fk.CardUseFinished},
  can_trigger = function (self, event, target, player, data)
    if target ~= player or not player:hasSkill(self) then return false end
    local card_type = data.card.type
    local room = player.room
    local logic = room.logic
    local use_event = logic:getCurrentEvent()
    local mark_name = "shujian_" .. data.card:getTypeString() .. "-turn"
    local mark = player:getMark(mark_name)
    if mark == 0 then
      logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
        local last_use = e.data[1]
        if last_use.from == player.id and last_use.card.type == card_type then
          mark = e.id
          room:setPlayerMark(player, mark_name, mark)
          return true
        end
        return false
      end, Player.HistoryTurn)
    end
    return mark == use_event.id and player.phase ~= Player.NotActive
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = table.map(room.alive_players, Util.IdMapper)
    local tos = room:askForChoosePlayers(player, targets, 1, 1, "#ld__n_shujian-choose", self.name)
    if #tos > 0 then
      self.cost_data = tos[1]
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data)
    to:drawCards(1,self.name)
    if player:usedSkillTimes(self.name, Player.HistoryTurn) < 3 then
       room:askForDiscard(to, 1, 1, true, self.name, false)
    elseif player:usedSkillTimes(self.name, Player.HistoryTurn) == 3 then
      H.doHideGeneral(room, player, player, self.name)
     end
  end,
  refresh_events = {fk.AfterCardUseDeclared},
  can_refresh = function(self, event, target, player, data)
    return player == target and player:hasSkill(self, true)
  end,
  on_refresh = function(self, event, target, player, data)
    local typesRecorded = player:getTableMark("@[cardtypes]shujian-turn")
    table.insertIfNeed(typesRecorded, data.card.type)
    player.room:setPlayerMark(player, "@[cardtypes]shujian-turn", typesRecorded)
  end,
}
local xiulv = fk.CreateTriggerSkill{
  name = "ld__n_xiulv",
  anim_type = "special",
  relate_to_place = "m",
  events = {fk.EventPhaseStart,fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return false end
    if event == fk.EventPhaseStart then
    local card = Fk:cloneCard("await_exhausted")
    return player == target and player.phase == Player.Finish and not player:prohibitUse(card)
    else
      if H.getGeneralsRevealedNum(player) ~= 2 then
      local cards = {}
      for _, move in ipairs(data) do
        if move.from ~= player.id and move.moveReason == fk.ReasonDiscard and move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
              table.insertIfNeed(cards, info.cardId)
            end
          end
        end
      end
      cards = table.filter(cards, function(id) return player.room:getCardArea(id) == Card.DiscardPile end)
      cards = U.moveCardsHoldingAreaCheck(player.room, cards)
      if #cards > 0 then
        self.cost_data = {cards = cards}
        return true
      end
    end
  end
end,
on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
    local targets = table.filter(room.alive_players, function(p) return
    H.compareKingdomWith(p, player) end)
    room:useVirtualCard("await_exhausted", {}, player, targets, self.name)
    else
    local all_choices = {"revealMain:::" .. player:getMark("__heg_general"), "revealDeputy:::" .. player:getMark("__heg_deputy"), "Cancel"}
    if player:prohibitReveal() or player.general ~= "anjiang" then
       table.removeOne(all_choices, "revealMain:::" .. player:getMark("__heg_general"))
    end
    if player:prohibitReveal(true) or player.deputyGeneral ~= "anjiang" then
      table.removeOne(all_choices, "revealDeputy:::" .. player:getMark("__heg_deputy"))
    end
    if #all_choices == 0 then return end
    local choice = room:askForChoice(player, all_choices, self.name)
    if choice == "revealMain:::" .. player:getMark("__heg_general") then
          player:revealGeneral(false)
    elseif choice =="revealDeputy:::" .. player:getMark("__heg_deputy") then
          player:revealGeneral(true)
    elseif choice == "Cancel" then return true end
    local cards = self.cost_data.cards
    --分类别获取
    local categorized = {
      basic = {},
      trick = {},
      equip = {}
    }
    for _, id in ipairs(cards) do
      local c = Fk:getCardById(id)
      local t = c:getTypeString()
      if categorized[t] then
        table.insert(categorized[t], id)
        end
      end
      -- 从每个类别取一张
      local selected = {}
      for _, t in pairs(categorized) do
      if #t > 0 then
      local card, _ = U.askforChooseCardsAndChoice(player, t, {"OK"}, self.name,
      "#ld__n_xiulv-choose", {"Cancel"}, 1, 1)
      if #card > 0 then
        table.insert(selected, card)
         end
       end
      end
      if #selected > 0 then
        room:obtainCard(player, selected, true, fk.ReasonPrey)
      end
    end
  end,
}
local pozhu = fk.CreateTriggerSkill{
  name = "ld__n_pozhu",
  anim_type = "special",
  events = {fk.GeneralRevealed},
  relate_to_place = "d",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player == target and player:getMark("@ld__n_pozhu-turn") == 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, "@ld__n_pozhu-turn", 1)
  end,
}
local pozhu_targetmod = fk.CreateTargetModSkill{
  name = "#ld__n_pozhu_targetmod",
  frequency = Skill.Compulsory,
  bypass_times = function(self, player, skill, scope, card)
    if card and player:hasSkill(self) and skill.trueName == "slash_skill"
      and scope == Player.HistoryPhase then
      return player:getMark("@ld__n_pozhu-turn") > 0
    end
  end,
}
duyu:addSkill(shujian)
duyu:addSkill(xiulv)
pozhu:addRelatedSkill(pozhu_targetmod)
duyu:addSkill(pozhu)
Fk:loadTranslationTable{
  ["ld__n_duyu"] = "杜预", --杜预
  ["#ld__n_duyu"] = "文成武德", --文成武德
  ["designer:ld__n_duyu"] = "",
  ["illustrator:ld__n_duyu"] = "",

  ["ld__n_shujian"] = "疏谏",
  [":ld__n_shujian"] = "当你于回合内首次使用一种类别的牌后，你可以令一名角色摸一张牌，然后若你本回合发动此技能的次数：小于3，其弃置一张牌；为3，你暗置一张武将牌。",
  ["ld__n_xiulv"] = "修律",
  [":ld__n_xiulv"] = "主将技，此武将牌上单独的阴阳鱼个数-1；结束阶段，你可以视为使用一张【以逸待劳】。其他角色因弃置而失去牌后，你可以明置一张武将牌并获得其中每种类别的牌各一张。",
  ["ld__n_pozhu"] = "破竹",
  [":ld__n_pozhu"] = "副将技，若你本回合明置过武将牌，你使用【杀】无次数限制。",

  ["@[cardtypes]shujian-turn"] = "疏谏",
  ["#ld__n_shujian-choose"] = "疏谏：选择一名角色摸一张牌",
  ["#ld__n_xiulv-choose"] = "修律：选择一张牌获得",
  ["@ld__n_pozhu-turn"] = "破竹 杀无次数限制",

  ["~ld__n_duyu"] = "",
}

local yanghu = General(extension, "ld__n_yanghuS", "jinz", 4)
yanghu.deputyMaxHpAdjustedValue = -1
yanghu:addCompanions("hs__n_duyu")
local zhaode = fk.CreateTriggerSkill{
  name = "ld__n_zhaode",
  anim_type = "offensive",
  events = {fk.Damage},
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(self) and not data.to.dead and not player:isKongcheng()
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = data.to
    local cards = room:askForCard(player, 1, player:getHandcardNum(), false, self.name, true, ".|.|spade,club|hand|.|.|.", "#ld__n_zhaode_card-choose")
    if #cards > 0 then
    room:showCards(cards, player)
    local card, _ = U.askforChooseCardsAndChoice(to, cards, {"OK"}, self.name,
    "#ld__n_zhaode_target-choose", {"Cancel"}, 1, #cards)
    if #card > 0 then
      room:obtainCard(to, card, true)
      local choices = {"ld__n_zhaode-hidden","ld__n_zhaode-drawcards"}
      local choice = room:askForChoice(player, choices, self.name, "#ld__n_zhaode-choose")
      if choice == "ld__n_zhaode-hidden" then
      H.doHideGeneral(room, player, player, self.name)
      else
        local num = ( player.maxHp - player:getHandcardNum())
        if num > 0 then
          player:drawCards(num, self.name)
        end
        room:invalidateSkill(player, "ld__n_zhaode", "-turn")
        room:setPlayerMark(player, "@ld__n_zhaode_prohibit-turn", 1)
        end
      end
    end
  end,
}
local qingke = fk.CreateTriggerSkill{
  name = "ld__n_qingke",
  relate_to_place = "d",
  events = {fk.TargetSpecified},
  can_trigger = function (self, event, target, player, data)
  if not (target == player and player:hasSkill(self) and player.phase ~= Player.NotActive and (data.card.trueName == "slash" or (data.card.color == Card.Black and data.card:isCommonTrick()))) then return end
  if H.getGeneralsRevealedNum(player) ~= 2 and #AimGroup:getAllTargets(data.tos) == 1 then
    local to = player.room:getPlayerById(data.to)
    return not to.dead
   end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local all_choices = {"revealMain:::" .. player:getMark("__heg_general"), "revealDeputy:::" .. player:getMark("__heg_deputy")}
    if player:prohibitReveal() or player.general ~= "anjiang" then
       table.removeOne(all_choices, "revealMain:::" .. player:getMark("__heg_general"))
    end
    if player:prohibitReveal(true) or player.deputyGeneral ~= "anjiang" then
      table.removeOne(all_choices, "revealDeputy:::" .. player:getMark("__heg_deputy"))
    end
    if #all_choices == 0 then return end
    local choice = room:askForChoice(player, all_choices, self.name)
    if choice == "revealMain:::" .. player:getMark("__heg_general") then
          player:revealGeneral(false)
    elseif choice =="revealDeputy:::" .. player:getMark("__heg_deputy") then
          player:revealGeneral(true)
    end
    local to = room:getPlayerById(data.to)
    data.additionalEffect = (data.additionalEffect or 0) + (2 - H.getGeneralsRevealedNum(to))
  end,
}
local chonge = H.CreateArraySummonSkill{
  name = "ld__n_chonge",
  array_type = "formation",
  relate_to_place = "m",
}
local chonge_trig = fk.CreateTriggerSkill{
  name = "#ld__n_chonge_trig",
  visible = false,
  events = {fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
      local to = player.room:getPlayerById(data.to)
      return data.card.trueName == "slash" and ( H.getGeneralsRevealedNum(to) ~= H.getGeneralsRevealedNum(target))
      and H.inFormationRelation(target, player) and player:hasSkill("ld__n_chonge")
      and player:hasShownSkill("ld__n_chonge")
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    data.disresponsiveList = data.disresponsiveList or {}
    table.insert(data.disresponsiveList, data.to)
  end,
}
yanghu:addSkill(zhaode)
yanghu:addSkill(qingke)
chonge:addRelatedSkill(chonge_trig)
yanghu:addSkill(chonge)
Fk:loadTranslationTable{
  ["ld__n_yanghuS"] = "羊祜", --羊祜
  ["#ld__n_yanghuS"] = "静水沧笙", --静水沧笙
  ["designer:ld__n_yanghuS"] = "",
  ["illustrator:ld__n_yanghuS"] = "",

  ["ld__n_zhaode"] = "昭德",
  [":ld__n_zhaode"] = "当你对其他角色造成伤害后，你可以展示任意张黑色手牌，然后其可以获得其中任意张牌并令你选择一项：1.你暗置一张武将牌；2.你将手牌摸至体力上限，然后此技能本回合失效。",
  ["ld__n_qingke"] = "清克",
  [":ld__n_qingke"] = "副将技，此武将牌上单独的阴阳鱼个数-1；当你于回合内使用【杀】或黑色普通锦囊牌指定唯一目标后，你可以明置一张武将牌并令此牌额外结算X次（X为目标角色的暗置武将牌数）。",
  ["ld__n_chonge"] = "冲轭",
  [":ld__n_chonge"] = "主将技，阵法技，与你处于同一队列的角色使用的【杀】不能被明置武将牌数不等于其的角色响应。",
  ["ld__n_zhaode-hidden"] = "暗置一张武将牌",
  ["ld__n_zhaode-drawcards"] = "将手牌摸至体力上限，然后此技能本回合失效",
  ["#ld__n_zhaode-choose"] = "昭德: 清选择一项",
  ["@ld__n_zhaode_prohibit-turn"] = "昭德 失效",
  ["#ld__n_zhaode_card-choose"] = "昭德：请选择任意张黑色手牌展示",
  ["#ld__n_zhaode_target-choose"] = "昭德：清选择任意张牌获得",
  ["#ld__n_chonge_trig"] = "冲轭",

  ["~ld__n_yanghu"] = "",
}

local zhangle = General(extension, "ld__n_zhangle", "jinz", 4)
local guoyi = fk.CreateActiveSkill{
  name = "ld__n_guoyi",
  prompt = "#ld__n_guoyi",
  anim_type = "offensive",
  min_card_num = 1,
  min_target_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = function (self, to_select, selected, player)
    return table.contains(player:getCardIds("h"), to_select) and not player:prohibitDiscard(Fk:getCardById(to_select))
    and Fk:getCardById(to_select).type == Card.TypeBasic
  end,
  target_filter = function (self, to_select, selected, selected_cards, _, _, player)
    local room = Fk:currentRoom()
    local target = room:getPlayerById(to_select)
    return to_select ~= player.id and #selected <= #selected_cards and not target:isNude() and
    table.every(selected, function(id) return (not H.compareKingdomWith(target, room:getPlayerById(id) or target.kingdom == "unknown")) end)
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    room:sortPlayersByAction(effect.tos)
    local cards = effect.cards
    local x,y= 0,0
    if #cards > 0 then
      room:throwCard(effect.cards, self.name, player)
      for _, id in ipairs(effect.tos) do
        if player.dead then break end
        local target = room:getPlayerById(id)
        local card = room:askForDiscard(target, 1, 1, true, self.name, true, ".|.|.|.|.|equip|.", "#ld__n_guoyi-card:"..player.id)
        if #card > 0 then
          x = x + 1
        else
          local cardz = room:askForCardChosen(player, target, "h", self.name)
            room:obtainCard(player, cardz, false, fk.ReasonPrey)
            y = y + 1
        end
      end
    end
    if x ~= 0 and y ~= 0 then
    local target = room:askForChoosePlayers(player, effect.tos, 1, 1, "#ld__n_guoyi-choose-damage")
      if #target > 0 then
          room:damage{
            from = player,
            to = room:getPlayerById(target[1]),
            damage = 1,
            skillName = self.name
          }
      end
    end
  end,
}
zhangle:addSkill(guoyi)
Fk:loadTranslationTable{
  ["ld__n_zhangle"] = "张虎乐綝", --张虎乐綝
  ["#ld__n_zhangle"] = "骁烈遗风", --骁烈遗风
  ["designer:ld__n_zhangle"] = "",
  ["illustrator:ld__n_zhangle"] = "",

  ["ld__n_guoyi"] = "果毅",
  [":ld__n_guoyi"] = "出牌阶段限一次，你可以弃置任意张基本牌，令至多等量名势力各不相同或未确定势力的其他角色各选择一项：1.弃置一张装备牌；2.你获得其一张手牌。若如此做，若这两项此次均被选择过，你可以对你选择的其中一名角色造成1点伤害。",
  ["#ld__n_guoyi"] = "果毅: 你可以弃置任意张基本牌并选择至多等量名势力各不相同或未确定势力的其他角色",
  ["#ld__n_guoyi-card"] = "果毅: 弃置一张装备牌，否则 %src获得你一张手牌",
  ["#ld__n_guoyi-choose-damage"] = "果毅: 选择一名角色，你对其造成1点伤害",

  ["~ld__n_zhangle"] = "",
}

local wenyang = General(extension, "ld__n_wenyang", "jinz", 4)
wenyang.hidden = true
local pimi = fk.CreateTriggerSkill{
  name = "ld__n_pimi",
}
local qingtian = fk.CreateTriggerSkill{
  name = "ld__n_qingtian",
}
wenyang:addSkill(pimi)
wenyang:addSkill(qingtian)
Fk:loadTranslationTable{
  ["ld__n_wenyang"] = "文鸯", --文鸯
  ["#ld__n_wenyang"] = "万人之雄", --万人之雄
  ["designer:ld__n_wenyang"] = "",
  ["illustrator:ld__n_wenyang"] = "",
  
  ["ld__n_pimi"] = "披靡",
  [":ld__n_pimi"] = "出牌阶段开始时，你可以令你此阶段使用牌不能被抵消，若如此做：1.其他角色于此阶段内成为你使用牌的目标时，其可以弃置两张与此牌颜色相同的牌，取消之；2.此回合结束时，若你此回合未造成过伤害，你弃置所有手牌。",
  ["ld__n_qingtian"] = "擎天",
  [":ld__n_qingtian"] = "每回合限一次，当你造成伤害时，你可以令此伤害+1，然后若存在与你势力相同或未确定势力的其他角色，你移除此武将牌。",

  ["~ld__n_wenyang"] = "",
}

local xiahouhui = General(extension, "ld__n_xiahouhui", "jinz", 3, 3, General.Female)
xiahouhui.hidden = true
xiahouhui:addCompanions("ld__n_simashi")
local shidu = fk.CreateActiveSkill{
  name = "ld__n_shidu",
}
local yishi = fk.CreateActiveSkill{
  name = "ld__n_yishi",
}
xiahouhui:addSkill(shidu)
xiahouhui:addSkill(yishi)
Fk:loadTranslationTable{
  ["ld__n_xiahouhui"] = "夏侯徽", --夏侯徽
  ["#ld__n_xiahouhui"] = "修容雅识", --修容雅识
  ["designer:ld__n_xiahouhui"] = "",
  ["illustrator:ld__n_xiahouhui"] = "",
  
  ["ld__n_shidu"] = "识度",
  [":ld__n_shidu"] = "每回合限一次，与你势力相同的角色使用基本牌或普通锦囊牌指定目标时，你可以为此牌额外指定一个目标或取消此牌的一个目标，此牌结算后你失去1点体力或弃置一个区域内的所有牌。",
  ["ld__n_yishi"] = "宜室",
  [":ld__n_yishi"] = "一名角色明置武将牌后，若其与你势力相同且其武将牌均明置，你可以令你或其回复1点体力。",

  ["~ld__n_xiahouhui"] = "",
}

local zhanghua = General(extension, "ld__n_zhanghua", "jinz", 3)
---获得场上所有能被利用的武器、防具、宝物的名字
---@param room AbstractRoom
---@param player_id integer
---@return string[]
local getOtherEffectiveEquip =  function(room, player_id) 
  local equip = {}
  for _, p in ipairs(room.alive_players) do
    if p.id == player_id then goto continue end
    local c = p:getEquipment(Card.SubtypeWeapon)
    if c then
      table.insertIfNeed(equip,Fk:getCardById(c).name)
    end
    c = p:getEquipment(Card.SubtypeArmor)
    if c then
      table.insertIfNeed(equip,Fk:getCardById(c).name)
    end
    c = p:getEquipment(Card.SubtypeTreasure)
    if c then
      table.insertIfNeed(equip,Fk:getCardById(c).name)
    end
    ::continue::
  end
  return equip
end

---获得场装备的拥有者
---@param room Room
---@param player ServerPlayer
---@param equipName string
---@return integer[] @ 拥有者（可能有多个）
local findOwnersFromOther =  function(room, player, equipName) 
  local r = {}
  for _, player in ipairs(room:getOtherPlayers(player)) do
    local c = table.find(table.map(player:getCardIds("e"),Util.Id2CardMapper),function (card)
      return card.name == equipName
    end)
    if c then
      table.insertIfNeed(r, player.id)
    end
  end
  return r
end

---让装备者选择是否重铸装备
---@param room Room
---@param equipName string @ 要重铸的装备名称
---@param player ServerPlayer @ 要排除的玩家
---@param skill_name string @ 技能名
---@return boolean? @ 是否成功找到玩家
local askForRecastEquip = function (room, equipName, player, skill_name)
  local targets = findOwnersFromOther(room, player, equipName)
  local card = nil
  local t = nil
  if #targets == 0 then
    return false  
  elseif #targets == 1 then
    t = room:getPlayerById(targets[1])
    card = room:askForCard(t, 1, 1, true, skill_name, true, equipName.. "|.|.|equip", "#ld__n_bowu_chooseToRecast:::"..equipName)
    if #card > 0 then
      room:recastCard(card,t,equipName)
    end
  else
    for _,t in pairs(targets) do
      local tar = room:getPlayerById(t)
      card = room:askForCard(tar, 1, 1, true, skill_name, true, equipName.."|.|.|equip", "#ld__n_bowu_chooseToRecast:::"..equipName)
      if #card > 0 then
        room:recastCard(card, tar, skill_name)
      end
    end
  end
  return true
end

local bowu = fk.CreateViewAsSkill{
  name = "ld__n_bowu",
  anim_type = "control",
  interaction = function (self, player)
    local equips = getOtherEffectiveEquip(Fk:currentRoom(), Self.id)
    local all_choices = {"crossbow", "spear", "ze_luminous_pearl", "luminous_pearl"}
    if Fk.currentResponsePattern then 
      all_choices = {"spear"}
    end
    local choices = table.filter(all_choices, function (equip)
      return table.contains(equips, equip)
    end)
    return UI.ComboBox{
      choices = choices
    }
  end,
  card_filter = function (self, to_select, selected, player)
    if self.interaction.data == "crossbow" then
      if #selected > 0 then return end
      return Fk:getCardById(to_select).trueName == "slash"
    elseif self.interaction.data == "spear" then
      return #selected < 2 and table.contains(player:getCardIds("h"), to_select)
    elseif self.interaction.data == "luminous_pearl" or self.interaction.data == "ze_luminous_pearl" then
      return #selected  < 3
    end
  end,
  view_as = function (self, cards, player)
    if #cards == 0 then return end
    if self.interaction.data == "crossbow" then
      local choose = Fk:getCardById(cards[1])
      choose.extra_data = choose.extra_data or {}
      choose.extra_data.bowu_crossbow = true
      return choose
    elseif self.interaction.data == "spear" then
      if #cards < 2 then return end
      local c = Fk:cloneCard("slash")
      c:addSubcards(cards)
      c.skillName = "spear_skill"
      c.skillName = self.name
      return c
    elseif self.interaction.data == "luminous_pearl" or self.interaction.data == "ze_luminous_pearl"  then
      local c = Fk:cloneCard(self.interaction.data)
      c:addSubcards(cards)
      return c
    end
  end,
  before_use = function (self, player, use)
    if self.interaction.data == "luminous_pearl" or self.interaction.data == "ze_luminous_pearl" then
      player.room:throwCard(use.card.subcards,self.interaction.data,player)
      if player.dead then return "" end
      player:drawCards(#use.card.subcards)
      return ""
    end
  end,
  after_use = function (self, player, use)
    askForRecastEquip(player.room, self.interaction.data, player, self.name)
  end,
  enabled_at_play = function (self, player)
    local equips = getOtherEffectiveEquip(Fk:currentRoom(), Self.id)
    local all_choices = {"crossbow", "spear", "ze_luminous_pearl", "luminous_pearl"}
    local choices = table.filter(all_choices, function (equip)
      return table.contains(equips, equip)
    end)
    return player:usedSkillTimes(self.name) < 1 and #choices > 0
  end,
  enabled_at_response = function (self, player)
    return player:usedSkillTimes(self.name) < 1 
           and Exppattern:Parse(Fk.currentResponsePattern):match(Fk:cloneCard("slash")) 
           and table.contains(getOtherEffectiveEquip(Fk:currentRoom(), player.id), "spear")
  end,
}
local bowu_targetMod = fk.CreateTargetModSkill{
  name = "#ld__n_bowu_targetMod",
  bypass_times = function (self, player, skill, scope, card, to)
    if not card or card.name ~= "slash" then return end
    if card.extra_data and card.extra_data.bowu_crossbow then
      return true
    end
  end
}
local bowu_trigger = fk.CreateTriggerSkill{
  name = "#ld__n_bowu_trigger",
  anim_type = "control",
  events = {fk.TargetSpecified, fk.DamageCaused, fk.CardEffectCancelledOut, 
            fk.AfterCardTargetDeclared, fk.CardUsing, fk.Damage,
            fk.AskForCardUse, fk.AskForCardResponse, fk.PreCardEffect, 
            fk.DamageInflicted, fk.EnterDying, fk.DrawNCards, fk.EventPhaseStart,
            fk.TargetConfirming, fk.BeforeChainStateChange},
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(self) or player:usedSkillTimes("ld__n_bowu",Player.HistoryTurn) >= 1 then return false end
    local use_cards = {}
    if event == fk.TargetSpecified then
      local to = player.room:getPlayerById(data.to)
      -- 青釭剑
      if (target == player and player:hasSkill(self) and data.card and data.card.trueName == "slash" and
      not to.dead) and #findOwnersFromOther(player.room, player, "qinggang_sword") > 0 then
        table.insert(use_cards, "qinggang_sword")
      end
      -- 雌雄双股剑
      if target == player and player:hasSkill(self) 
         and data.card and data.card.trueName == "slash" 
         and player:compareGenderWith(to, true)
         and #findOwnersFromOther(player.room, player, "double_swords") > 0  then
        table.insert(use_cards, "double_swords")
      end
      -- 飞龙夺凤
      if target == player and data.card and data.card.trueName == "slash" 
         and not to:isNude()
         and #findOwnersFromOther(player.room, player, "dragon_phoenix") > 0 then
          table.insert(use_cards, "dragon_phoenix")
      end
    elseif event == fk.DamageCaused then
      -- 寒冰剑
      if target == player and player:hasSkill(self) and (not data.chain) 
         and data.card and data.card.trueName == "slash" and not data.to:isNude()
         and #findOwnersFromOther(player.room, player, "ice_sword") > 0  then
        table.insert(use_cards, "ice_sword")
      end
      -- 麒麟弓
      local ret = target == player and player:hasSkill(self) and
      data.card and data.card.trueName == "slash" and (not data.chain)
      if ret then
        local to = data.to
        if table.find(to:getCardIds(Player.Equip), function (id)
          local card = Fk:getCardById(id)
          return card.sub_type == Card.SubtypeDefensiveRide or card.sub_type == Card.SubtypeOffensiveRide
        end) and #findOwnersFromOther(player.room, player, "kylin_bow") > 0 then
          table.insert(use_cards, "kylin_bow")
        end
      end
    elseif event == fk.CardEffectCancelledOut then
      -- 贯石斧
      if player:hasSkill(self) and data.from == player.id and data.card.trueName == "slash" 
         and not player.room:getPlayerById(data.to).dead
         and #findOwnersFromOther(player.room, player, "axe") > 0 then
        table.insert(use_cards, "axe")
      end
    elseif event == fk.AfterCardTargetDeclared then
      -- 方天画戟
      if target == player and player:hasSkill(self) and data.card.trueName == "slash" and #player.room:getUseExtraTargets(data) > 0 
         and #findOwnersFromOther(player.room, player, "sa__halberd") > 0 then
          table.insert(use_cards, "sa__halberd")
      end
      -- 朱雀羽扇
      if target == player and player:hasSkill(self) and data.card.name == "slash" and #findOwnersFromOther(player.room, player, "fan") > 0 then
        table.insert(use_cards, "fan")
      end
    elseif event == fk.CardUsing then
      -- 青龙偃月刀
      if target == player and player:hasSkill(self) and data.card.trueName == "slash" 
         and #findOwnersFromOther(player.room, player, "sa__blade") > 0 then
        table.insert(use_cards, "blade")
      end
    elseif event == fk.Damage then
      -- 三尖两刃刀
      if target == player and player:hasSkill(self) and data.card and data.card.trueName == "slash" and not data.to.dead and not data.chain and
         not player:isKongcheng() and table.find(player.room.alive_players, function(p) return data.to:distanceTo(p) == 1 and p ~= player end)
         and #findOwnersFromOther(player.room, player, "triblade") > 0 then
        table.insert(use_cards, "triblade")
      end
    elseif event == fk.AskForCardResponse then
      -- 八卦阵
      if (target == player and player:hasSkill(self) 
          and (data.cardName == "jink" or (data.pattern and Exppattern:Parse(data.pattern):matchExp("jink|0|nosuit|none"))))
          and not player:prohibitResponse(Fk:cloneCard("jink")) 
          and #findOwnersFromOther(player.room, player, "eight_diagram") > 0 then
        table.insert(use_cards, "eight_diagram")
      end
    elseif  event == fk.AskForCardUse then
      -- 八卦阵
      if (target == player and player:hasSkill(self) 
          and (data.cardName == "jink" or (data.pattern and Exppattern:Parse(data.pattern):matchExp("jink|0|nosuit|none"))))
          and not player:prohibitUse(Fk:cloneCard("jink")) 
          and #findOwnersFromOther(player.room, player, "eight_diagram") > 0 then
        table.insert(use_cards, "eight_diagram")
      end
    elseif event == fk.PreCardEffect then
      -- 仁王盾
      if player.id == data.to and player:hasSkill(self) 
         and data.card.trueName == "slash" and data.card.color == Card.Black
         and #findOwnersFromOther(player.room, player, "nioh_shield") > 0 then
        table.insert(use_cards, "nioh_shield")
      end
      -- 藤甲
      if player.id == data.to and player:hasSkill(self)
         and (data.card.name == "slash" or data.card.name == "savage_assault"
              or data.card.name == "archery_attack") 
         and #findOwnersFromOther(player.room, player, "vine") > 0 then
        table.insert(use_cards, "vine")
      end
    elseif event == fk.DamageInflicted then
      -- 白银狮子
      if target == player and player:hasSkill(self) and data.damage > 1 and #findOwnersFromOther(player.room, player, "silver_lion") > 0 then
        table.insert(use_cards, "silver_lion")
      end
      -- 藤甲
      if player.id == data.to and player:hasSkill(self)
         and (data.card.name == "slash" or data.card.name == "savage_assault"
              or data.card.name == "archery_attack")
         and #findOwnersFromOther(player.room, player, "vine") > 0 then
        table.insert(use_cards, "vine")
      end
      -- 太平要术
      if target == player and player:hasSkill(self) and data.damageType ~= fk.NormalDamage and #findOwnersFromOther(player.room, player, "peace_spell") > 0 then
        table.insert(use_cards, "peace_spell")
      end
    elseif event == fk.EnterDying then
      -- 飞龙夺凤
      if data.damage and data.damage.from == player and not target:isKongcheng() 
         and player.room.logic:damageByCardEffect() and data.damage.card.trueName == "slash"
         and #findOwnersFromOther(player.room, player, "dragon_phoenix") > 0 then
        table.insert(use_cards, "dragon_phoenix")
      end
    elseif event == fk.EventPhaseStart then
      -- 玉玺
      if player.phase == Player.Play and target == player and player:hasSkill(self) and H.getKingdom(player) ~= "unknown" 
         and #findOwnersFromOther(player.room, player, "jade_seal") > 0 then
        table.insert(use_cards, "jade_seal")
      end
    elseif event == fk.DrawNCards then
      -- 玉玺
      if target == player and player:hasSkill(self) and H.getKingdom(player) ~= "unknown" 
         and #findOwnersFromOther(player.room, player, "jade_seal") > 0 then
        table.insert(use_cards, "jade_seal")
      end
    elseif event == fk.TargetConfirming then
      -- 明光铠
      if target == player and player:hasSkill(self) and table.contains({"fire__slash", "burning_camps", "fire_attack"}, data.card.name)
         and #findOwnersFromOther(player.room, player, "iron_armor") > 0 then
        table.insert(use_cards, "iron_armor")
      end
    elseif event == fk.BeforeChainStateChange then
      -- 明光铠
      if target == player and player:hasSkill(self) and H.isSmallKingdomPlayer(player) and not player.chained
         and #findOwnersFromOther(player.room, player, "iron_armor") > 0 then
        table.insert(use_cards, "iron_armor")
      end
    end
    if #use_cards == 0 then return end
    self.cost_data = {}
    self.cost_data.use_equips = use_cards;
    return true
  end,
  on_cost = function (self, event, target, player, data)
    local choices = self.cost_data.use_equips
    local room = player.room
    table.insert(choices, #choices + 1, "Cancel")
    local choose = player.room:askForChoice(player, choices, self.name, "#ld__n_bowu_chooseToUse")
    if choose ~= "Cancel" then
      self.cost_data.use = choose
      -- 贯石斧
      if choose == "axe" then
        local room = player.room
        local cards = {}
        for _, id in ipairs(player:getCardIds("he")) do
          if not player:prohibitDiscard(id) and
            not (table.contains(player:getEquipments(Card.SubtypeWeapon), id) and Fk:getCardById(id).name == "axe") then
            table.insert(cards, id)
          end
        end
        cards = room:askForDiscard(player, 2, 2, true, self.name, true, ".|.|.|.|.|.|"..table.concat(cards, ","),
          "#axe-invoke::"..data.to, true)
        if #cards > 0 then
          self.cost_data.cards = cards
          player:revealBySkillName(bowu.name)
          return true
        end
      -- 方天画戟
      elseif choose == "sa__halberd" then
        room:setPlayerMark(player, "_sa__halberd", TargetGroup:getRealTargets(data.tos))
        local _, ret = room:askForUseActiveSkill(player, "#sa__halberd_targets", "#sa__halberd-ask", true)
        room:setPlayerMark(player, "_sa__halberd", 0)
        if ret then
          self.cost_data.retTarget = ret.targets
          player:revealBySkillName(bowu.name)
          return true
        end
      -- 三尖两刃刀
      elseif choose == "triblade" then
        local targets = table.map(table.filter(room.alive_players, function(p)
          return data.to:distanceTo(p) == 1 and p ~= player end), Util.IdMapper)
        if #targets == 0 then return false end
        local to, card = room:askForChooseCardAndPlayers(player, targets, 1, 1, ".|.|.|hand", "#triblade-invoke::"..data.to.id, self.name, true)
        if #to > 0 then
          self.cost_data.toAndCards = {to[1], card}
          player:revealBySkillName(bowu.name)
          return true
        end
      else
        player:revealBySkillName(bowu.name)
        return true
      end
      return false
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local ret = false
    -- 青釭剑
    if self.cost_data.use == "qinggang_sword" then
      player.room:getPlayerById(data.to):addQinggangTag(data)
    -- 雌雄双股剑
    elseif self.cost_data.use == "double_swords" then
      local to = player.room:getPlayerById(data.to)
      if to:isKongcheng() then
        player:drawCards(1, self.name)
      else
        local result = room:askForDiscard(to, 1, 1, false, self.name, true, ".", "#double_swords-invoke:"..player.id)
        if #result == 0 then
          player:drawCards(1, self.name)
        end
      end
    -- 寒冰剑
    elseif self.cost_data.use == "ice_sword" then
      local to = data.to
      for i = 1, 2 do
        if player.dead or to.dead or to:isNude() then break end
        local card = room:askForCardChosen(player, to, "he", self.name)
        room:throwCard({card}, self.name, to, player)
      end
      ret = true
    -- 青龙偃月刀
    elseif self.cost_data.use == "blade" then
      for _, id in ipairs(TargetGroup:getRealTargets(data.tos)) do
        local p = room:getPlayerById(id)
        room:addPlayerMark(p, "@@sa__blade")
        local record = p:getTableMark(MarkEnum.RevealProhibited)
        table.insertTable(record, {"m", "d"})
        room:setPlayerMark(p, MarkEnum.RevealProhibited, record)
        data.extra_data = data.extra_data or {}
        data.extra_data.sa__bladeRevealProhibited = data.extra_data.sa__bladeRevealProhibited or {}
        data.extra_data.sa__bladeRevealProhibited[tostring(id)] = (data.extra_data.sa__bladeRevealProhibited[tostring(id)] or 0) + 1
      end
    -- 贯石斧
    elseif self.cost_data.use == "axe" then
      room:throwCard(self.cost_data.cards, "axe", player, player)
      ret = true
    -- 方天画戟
    elseif self.cost_data.use == "halberd" then
      room:doIndicate(player.id, self.cost_data.retTarget)
      data.extra_data = data.extra_data or {}
      data.extra_data.saHalberd = true
      data.card.skillName = "sa__halberd"
      room:sendLog{
        type = "#HalberdTargets",
        from = player.id,
        to = self.cost_data.retTarget,
        arg = "sa__halberd",
        card = Card:getIdList(data.card),
      }
      table.forEach(self.cost_data.retTarget, function (id)
        table.insert(data.tos, {id})
      end)
    -- 麒麟弓
    elseif self.cost_data.use == "kylin_bow" then
      local to = data.to
      local ride_tab = table.filter(to:getCardIds(Player.Equip), function (id)
        local card = Fk:getCardById(id)
        return card.sub_type == Card.SubtypeDefensiveRide or card.sub_type == Card.SubtypeOffensiveRide
      end)
      if #ride_tab == 0 then return end
      local id = room:askForCardChosen(player, to, {
        card_data = {
          { "equip_horse", ride_tab }
        }
      }, self.name)
      room:throwCard({id}, self.name, to, player)
    -- 三尖两刃刀
    elseif self.cost_data.use == "triblade" then
      local to = self.cost_data.toAndCards[1]
      room:throwCard(self.cost_data.toAndCards[2], self.name, player, player)
      room:damage{
        from = player,
        to = room:getPlayerById(to),
        damage = 1,
        skillName = self.name,
      }
    -- 朱雀羽扇
    elseif self.cost_data.use == "fan" then
      local card = Fk:cloneCard("fire__slash", data.card.suit, data.card.number)
      for k, v in pairs(data.card) do
        if card[k] == nil then
          card[k] = v
        end
      end
      if data.card:isVirtual() then
        card.subcards = data.card.subcards
      else
        card.id = data.card.id
      end
      card.skillNames = data.card.skillNames
      card.skillName = "fan"
      data.card = card
    -- 八卦阵
    elseif self.cost_data.use == "eight_diagram" then
      local room = player.room
      local judgeData = {
        who = player,
        reason = self.name,
        pattern = ".|.|heart,diamond",
      }
      room:judge(judgeData)

      if judgeData.card.color == Card.Red then
        if event == fk.AskForCardUse then
          data.result = {
            from = player.id,
            card = Fk:cloneCard('jink'),
          }
          data.result.card.skillName = "eight_diagram"

          if data.eventData then
            data.result.toCard = data.eventData.toCard
            data.result.responseToEvent = data.eventData.responseToEvent
          end
        else
          data.result = Fk:cloneCard('jink')
          data.result.skillName = "eight_diagram"
        end
        ret = true
      end
    -- 仁王盾
    elseif self.cost_data.use == "nioh_shield" then
      ret = true
    -- 白银狮子
    elseif self.cost_data.use == "silver_lion" then
      data.damage = 1
    -- 藤甲
    elseif self.cost_data.use == "vine" then
      room:sendLog{
        type = "#InvokeSkill",
        from = player.id,
        arg = self.name,
      }
      if event == fk.DamageInflicted then
        data.damage = data.damage + 1
      else
        ret = true
      end
    -- 飞龙夺凤
    elseif self.cost_data.use == "dragon_phoenix" then
      if event == fk.TargetSpecified then
        local to = player.room:getPlayerById(data.to)
        room:askForDiscard(to, 1, 1, true, self.name, false, ".", "#dragon_phoenix-invoke")
      else
        local card = room:askForCardChosen(player, target, "h", self.name)
        room:obtainCard(player, card, false, fk.ReasonPrey)
      end
    -- 太平要术
    elseif self.cost_data.use == "peace_spell" then
      ret = true
      -- 玉玺
    elseif self.cost_data.use == "jade_seal" then
      if event == fk.EventPhaseStart then
        local targets = table.map(self.cost_data.to, Util.Id2PlayerMapper)
        room:useVirtualCard("known_both", nil, player, targets, self.name)
      else
        data.n = data.n + 1
      end
    -- 明光铠
    elseif self.cost_data.use == "iron_armor" then
      if event == fk.TargetConfirming then 
        AimGroup:cancelTarget(data, player.id)
      end
      ret = true
    end
    player:addSkillUseHistory("ld__n_bowu", 1)
    askForRecastEquip(room,self.cost_data.use , player, self.name)
    if ret then
      return true
    end
  end,
  refresh_events = { fk.CardUseFinished },
  can_refresh = function(self, event, target, player, data)
    return data.extra_data and data.extra_data.sa__bladeRevealProhibited
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for key, num in pairs(data.extra_data.sa__bladeRevealProhibited) do
      local p = room:getPlayerById(tonumber(key))
      if p:getMark("@@sa__blade") > 0 then
        room:removePlayerMark(p, "@@sa__blade", num)
        local record = p:getTableMark(MarkEnum.RevealProhibited)
        table.removeOne(record, "m")
        table.removeOne(record, "d")
        if #record == 0 then record = 0 end
        room:setPlayerMark(p, MarkEnum.RevealProhibited, record)
      end
    end
    data.sa__bladeRevealProhibited = nil
  end,
}
local halberdDelay = fk.CreateTriggerSkill{
  name = "#ld__n_bowu_sa__halberd_delay",
  mute = true,
  events = {fk.CardEffectCancelledOut},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target == player and data.card.trueName == "slash" and (player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard).data[1].extra_data or {}).saHalberd
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local e = room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
    local use = e.data[1]
    if #TargetGroup:getRealTargets(use.tos) > 0 then
      room:sendLog{
        type = "#HalberdNullified",
        from = target.id,
        -- to = {player.id},
        arg = "sa__halberd",
        arg2 = data.card:toLogString(),
      }
      use.nullifiedTargets = TargetGroup:getRealTargets(use.tos)
    end
  end,
}
local fenghua = fk.CreateActiveSkill{
  name = "ld__n_fenghua",
  anim_type = "drawcard",
  can_use = function (self, player, card, extra_data)
    return player:usedSkillTimes(self.name) < 1
  end,
  card_filter = Util.FalseFunc,
  target_filter = Util.FalseFunc,
  on_use = function (self, room, effect)
    local player = room:getPlayerById(effect.from)
    local discards = {}
    for _, p in ipairs(room:getAlivePlayers()) do
      if H.compareKingdomWith(p, player) then
        p:drawCards(1, self.name)
      end
    end
    for _, p in ipairs(room:getAlivePlayers()) do
      if H.compareKingdomWith(p, player) then
        if p.dead then goto continue end
        local discard = room:askForDiscard(p, 1, 999, true, self.name, false, ".","#ld__n_fenghuaDiscard")
        table.insertTableIfNeed(discards, discard)
      end
      ::continue::
    end
    local suits = {}
    for _, c in ipairs(discards) do
      table.insertIfNeed(suits, Fk:getCardById(c).suit)
    end
    table.removeOne(suits, Card.NoSuit)
    if #suits >= 4 and player:getMark("@@ld__n_fenghua_transform") == 0 and room:askForChoice(player, {"transform_deputy", "Cancel"}, self.name) ~= "Cancel" then
      room:addPlayerMark(player, "@@ld__n_fenghua_transform")
      H.transformGeneral(room, player)
    end
  end,
}
bowu:addRelatedSkill(bowu_targetMod)
bowu:addRelatedSkill(bowu_trigger)
bowu:addRelatedSkill(halberdDelay)
zhanghua:addSkill(bowu)
zhanghua:addSkill(fenghua)
Fk:loadTranslationTable{
  ["ld__n_zhanghua"] = "张华",
  ["#ld__n_zhanghua"] = "清骨博志",
  ["designer:ld__n_zhanghua"] = "",
  ["illustrator:ld__n_zhanghua"] = "",
  
  ["ld__n_bowu"] = "博物",
  [":ld__n_bowu"] = "每回合限一次，你可以发动其他角色装备区内的武器、防具或宝物效果，然后拥有此牌的角色可以重铸之。",
  ["ld__n_fenghua"] = "风华",
  [":ld__n_fenghua"] = "出牌阶段限一次，你可以令所有与你势力相同角色依次摸一张牌，然后这些角色依次弃置至少一张牌，若以此法弃置的牌包含四种花色，你可以变更。",
  ["#ld__n_bowu_trigger"] = "博物",
  ["#ld__n_bowu_chooseToRecast"] = "博物：你可以重铸【%arg】",
  ["#ld__n_bowu_askToRecast"] = "博物：请选择一名角色，其可重铸【%arg】",
  ["#ld__n_bowu_chooseToUse"] = "博物：你可以发动一张装备牌的效果",
  ["@@ld__n_fenghua_transform"] = "风华 已变更",

  ["~ld__n_zhanghua"] = "",
}

--- 获取未确定势力角色数
--- @param room Room
--- @param include_dead boolean?
--- @return integer
local getUnknownKingdomPlayerNum = function (room, include_dead)
  include_dead = include_dead or false
  local ret = 0
  for _, p in ipairs(include_dead and room.players or room.alive_players) do
    if H.getKingdom(p) == "unknown" then
      ret = ret + 1
    end
  end
  return ret
end


local simayan = General(extension, "ld__n_simayan", "jinz", 4)
simayan:addCompanions("ld__n_zhanghua")
local zhaoshi = fk.CreateTriggerSkill{
  name = "ld__n_zhaoshi",
  anim_type = "special",
  events = {fk.TurnStart},
  frequency = Skill.Limited,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player == target and player.phase == Player.Play and
     table.find(player.room:getOtherPlayers(player, false), function(p) return H.getGeneralsRevealedNum(p) == 0 end)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local use = U.askForUseVirtualCard(room, player, "imperial_order", nil, self.name, "#ld__n_zhaoshi_prompt", true, true, true, true, nil, true)
    if use then
      self.cost_data = use
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:useCard(self.cost_data)
    local drawCardNum = getUnknownKingdomPlayerNum(room)
    player:drawCards(drawCardNum, self.name)
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if not p.dead and H.getKingdom(p) == "unknown" then
        p:drawCards(drawCardNum, self.name)
      end
    end
  end,
}
local yiye = fk.CreateTriggerSkill{
  name = "ld__n_yiye",
  anim_type = "special",
  frequency = Skill.Limited,
  events = {fk.RoundStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) 
           and not table.find(player.room:getOtherPlayers(player, false), function(p) return H.getGeneralsRevealedNum(p) == 0 end)
           and player:usedSkillTimes(self.name, Player.HistoryGame) < 1
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:changeMaxHp(player, 1)
    if not player.dead then
      room:recover({
        who = player,
        num = 1,
        recoverBy = player,
        skillName = self.name
      })
    end
    local seat = player.seat
    for _, p in ipairs(room:getAlivePlayers(false)) do
      if not p.dead and p.seat < seat then
        p:turnOver()
      end
    end
  end,
}
simayan:addSkill(zhaoshi)
simayan:addSkill(yiye)
Fk:loadTranslationTable{
  ["ld__n_simayan"] = "司马炎", --司马炎
  ["#ld__n_simayan"] = "",
  ["designer:ld__n_simayan"] = "",
  ["illustrator:ld__n_simayan"] = "",
  
  ["ld__n_zhaoshi"] = "昭始",
  [":ld__n_zhaoshi"] = "出牌阶段开始时，若场上有未确定势力的角色，你可以视为使用一张【敕令】，此牌结算后你与未确定势力的角色各摸X张牌（X为缺失的势力数）。",
  ["#ld__n_zhaoshi_prompt"] = "技能1：你可以视为使用一张【敕令】，此牌结算后你与未确定势力的角色各摸X张牌（X为缺失的势力数）。",

  ["ld__n_yiye"] = "懿业",
  [":ld__n_yiye"] = "限定技，每轮开始时，若场上所有角色势力均已确定，你可以加1点体力上限并回复1点体力，然后所有座次小于你的角色叠置。",

  ["~ld__n_simayan"] = "",
}


local yangjun = General(extension, "ld__n_yangjun", "jinz", 3)
local bizhuan = fk.CreateTriggerSkill{
  name = "ld__n_bizhuan",
  frequency = Skill.Compulsory,
  anim_type = "special",
  events = {fk.GeneralRevealed, fk.CardUsing, "fk.BeforeCommandUse"},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and event == "fk.BeforeCommandUse" and data.from == player and target.hp < player.hp and H.isBigKingdomPlayer(player) then
      return true 
    end
    if target ~= player or not player:hasSkill(self) then return false end
    if event == fk.GeneralRevealed then
      for _, v in pairs(data) do
        if table.contains(Fk.generals[v]:getSkillNameList(), self.name) then return true end
      end
    else
      return H.isBigKingdomPlayer(player)
    end
  end,
  on_cost = function (self, event, target, player, data)
    if event == fk.GeneralRevealed then
      for _, cid in ipairs(Fk:getAllCardIds()) do
        local card = Fk:getCardById(cid)
        if  card.name == "jade_seal" then
          local area = player.room:getCardArea(cid)
          if (area == Card.DiscardPile or area == Card.PlayerEquip or rea == Card.PlayerJudge) then
            self.cost_data = cid
            return true
          end
        end 
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GeneralRevealed then
      room:obtainCard(player, self.cost_data, true, fk.ReasonPrey, player.id, self.name)
    elseif event == fk.CardUsing then
      data.disresponsiveList  = data.disresponsiveList or {}
      for _, p in ipairs(room:getAllPlayers()) do
        if H.isSmallKingdomPlayer(p) then
          table.insertIfNeed(data.disresponsiveList, p.id)
        end
      end
    else
      data.forced = true
    end
  end,
}
local shechao = fk.CreateTriggerSkill{
  name = "ld__n_shechao",
  anim_type = "special",
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    if not (player:hasSkill(self) and player ~= target) then return false end
    local targets = {}
    local logic = player.room.logic
    logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
      local use = e.data[1]
      if use.from == target.id then
        for _, id in ipairs(TargetGroup:getRealTargets(use.tos)) do
          table.insertIfNeed(targets, id)
        end
        if #targets > 1 then return true end
      end
    end, Player.HistoryTurn)
    return #targets == 0 or (#targets == 1 and targets[1] == target.id)
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#ld__n_shechao_prompt::"..target.id)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if not H.askCommandTo(player, target, self.name) then
      if room:askForDiscard(player, 1, 1, true, self.name, false, ".", "#ld__n_shechao_emperor") then
        player:gainAnExtraTurn()
      end
    end
  end,
}
yangjun:addSkill(bizhuan)
yangjun:addSkill(shechao)
Fk:loadTranslationTable{
  ["ld__n_yangjun"] = "杨骏", --杨骏
  ["#ld__n_yangjun"] = "阶缘佞宠", --阶缘佞宠
  ["designer:ld__n_yangjun"] = "",
  ["illustrator:ld__n_yangjun"] = "",
  
  ["ld__n_bizhuan"] = "愎专",
  [":ld__n_bizhuan"] = "锁定技，当你明置此武将牌后，你获得场上或弃牌堆中的【玉玺】；若你为大势力角色，小势力角色不能响应你使用的牌且体力值小于你的角色强制执行你发起的“军令”。",
  ["ld__n_shechao"] = "摄朝",
  [":ld__n_shechao"] = "其他角色的回合结束时，若其本回合未对其以外的角色使用过牌，你可以对其发起“军令”，若其不执行，你执行【挟天子以令诸侯】的效果。",
  ["#ld__n_shechao_prompt"] = "摄朝：你可以对 %dest 发起 “军令”，若其不执行你执行【挟天子以令诸侯】效果",
  ["#ld__n_shechao_emperor"] = "摄朝：你须执行【挟天子以令诸侯】的效果，弃置一张手牌并执行一个额外的回合",

  ["~ld__n_yangjun"] = "",
}

local lordsimayan = General(extension, "ld__n_lordsimayan", "jinz", 4)
lordsimayan.hidden = true
local taishi = fk.CreateTriggerSkill{
  name = "ld__n_taishi",
}
local juyi = fk.CreateTriggerSkill{
  name = "ld__n_juyi",
}
local tianci = fk.CreateTriggerSkill{
  name = "ld__n_tianci",
}
lordsimayan:addSkill(taishi)
lordsimayan:addSkill(juyi)
lordsimayan:addSkill(tianci)
Fk:loadTranslationTable{
  ["ld__n_lordsimayan"] = "君司马炎", --君司马炎
  ["#ld__n_lordsimayan"] = "麒踏云汉", --麒踏云汉
  ["designer:ld__n_lordsimayan"] = "",
  ["illustrator:ld__lordsimayan"] = "",

  ["ld__n_taishi"] = "泰始",
  [":ld__n_taishi"] = "<b><font color='goldenrod'>君主技</font></b>，你拥有“八公同辰诏”。<br>" ..
  "#<b>八公同辰诏</b>：①若没有已死亡的晋势力角色，所有晋势力角色的攻击范围和手牌上限+1。<br>" ..
  "②晋势力角色死亡后，你将其所有武将牌置于“八公同辰诏”上，称为“王”。<br>"..
  "③晋势力角色的准备阶段，若“八公同辰诏”上有“王”且其没有“王”，你可以将一张“王”置于其武将牌旁，令其获得该“王”武将牌上的所有技能（无视主副将技标签）。",
  ["ld__n_tianci"] = "天赐",
  [":ld__n_tianci"] = "锁定技，①当你成为【赐】的目标时，你取消之；②当其他角色使用【赐】时，你摸一张牌；③你的♠基本牌或你判定牌中的♠基本牌均视为【赐】。",
  ["ld__n_juyi"] = "举弈",
  [":ld__n_juyi"] = "当你每回合体力首次变化后，你可以令当前回合角色的一张已明置武将牌上的一个非锁定技于本回合视为未发动过，且当其本回合下次发动该技能后，其失去1点体力。",
  
  ["ld__n_ci"] = "【赐】：出牌阶段，对一名其他角色使用。将此牌交给目标角色并令你本回合对其使用牌无距离和次数限制。", -- 替换 2【杀】1【闪】

  ["~ld__n_lordsimayan"] = "",
}

return extension
